package servlet;
import hibernate.Root_menu;
import hibernate.Tree_menu;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import DAO.GetMenuInfoDAO;
import DAO.UserDAO;

import com.et.mvc.JsonView;
import com.et.mvc.TextView;
import com.et.mvc.util.Json;

/**
 * Servlet implementation class getMenu
 */
public class getMenu extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public getMenu() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
        HttpSession session = request.getSession();
        int j = Integer.parseInt(session.getAttribute("role").toString());
        String userName = session.getAttribute("userName").toString();
		String tree_menu_id = request.getParameter("tree_menu_id");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/plain");
		PrintWriter out = response.getWriter();   
        JsonView getTree = null;       
		GetMenuInfoDAO getMenuInfo = new GetMenuInfoDAO();
		List getTreeList = getMenuInfo.getTree();
		if(tree_menu_id == null){			
			String temp="";
	        //通过用户名得到他的权限
			UserDAO u = new UserDAO();
			int[] r= u.getroleofuser(userName);
			//资产管理
	        if(r[0]==1||r[1]==1||r[2]==1||r[3]==1||r[4]==1||r[5]==1||r[6]==1
	        		||r[7]==1||r[8]==1||r[9]==1||r[10]==1||r[11]==1||r[12]==1
	        		||r[13]==1||r[14]==1||r[15]==1||r[16]==1||r[17]==1||r[18]==1||r[19]==1){
        	    //System.out.println("资产管理   "+((Tree_menu)getTreeList.get(0)).getId());
				temp = temp + "{id:'"+((Tree_menu)getTreeList.get(0)).getId()+"',";
				temp = temp + "title:'"+ ((Tree_menu)getTreeList.get(0)).getTitle()+"'},";				
	        }
	        /*//报表管理
	        if(r[20]==1||r[21]==1||r[22]==1||r[23]==1){
	        	//System.out.println("报表管理   "+((Tree_menu)getTreeList.get(2)).getId());
				temp = temp + "{id:'"+((Tree_menu)getTreeList.get(2)).getId()+"',";
			    temp = temp + "title:'"+ ((Tree_menu)getTreeList.get(2)).getTitle()+"'},";
	        }
	        //采购管理
	        if(r[24]==1||r[25]==1||r[26]==1||r[27]==1||r[28]==1){
	        	//System.out.println("采购管理    "+((Tree_menu)getTreeList.get(1)).getId());
				temp = temp + "{id:'"+((Tree_menu)getTreeList.get(1)).getId()+"',";
			    temp = temp + "title:'"+ ((Tree_menu)getTreeList.get(1)).getTitle()+"'},";
	        }
	        //字典管理
	        if(r[29]==1||r[30]==1){
	        	//System.out.println("字典管理    "+((Tree_menu)getTreeList.get(3)).getId());
				temp = temp + "{id:'"+((Tree_menu)getTreeList.get(3)).getId()+"',";
			    temp = temp + "title:'"+((Tree_menu)getTreeList.get(3)).getTitle()+"'},";
	        }*/
	        //用户管理   日志管理
	        if(r[31]==1||r[32]==1||r[33]==1||r[34]==1||r[35]==1||r[36]==1
	        		||r[37]==1||r[38]==1){
	        	//System.out.println("用户管理  日志管理  " +((Tree_menu)getTreeList.get(4)).getId());
/*				temp = temp + "{id:'"+((Tree_menu)getTreeList.get(4)).getId()+"',";
			    temp = temp + "title:'"+((Tree_menu)getTreeList.get(4)).getTitle()+"'},";*/
				temp = temp + "{id:'"+((Tree_menu)getTreeList.get(5)).getId()+"',";
			    temp = temp+ "title:'"+((Tree_menu)getTreeList.get(5)).getTitle()+"'},";
	        }
            
	        String res = temp.substring(0, temp.lastIndexOf(",")); 
			out.print("[" + res + "]");
	        out.flush();
		}
		else{
	        String result = "";
        	List getRootList = getMenuInfo.getRoot(tree_menu_id);		        
			UserDAO u = new UserDAO();
			int[] r= u.getroleofuser(userName);	
			//资产登记
			if(tree_menu_id.equals(((Tree_menu)getTreeList.get(0)).getId())){
				if(r[0]==1||r[1]==1||r[2]==1||r[3]==1){
					result = result + "{id:'" + ((Root_menu)getRootList.get(0)).getId()+"',";
					result = result + "text:'"+((Root_menu)getRootList.get(0)).getTitle() +"',";
					result = result + "url:'"+ ((Root_menu)getRootList.get(0)).getUrl() + "',";
					result = result + "leaf:true},";
				}
/*				//资产报损
				if(r[4]==1||r[5]==1||r[6]==1||r[7]==1){
					result = result + "{id:'" + ((Root_menu)getRootList.get(1)).getId()+"',";
					result = result + "text:'"+((Root_menu)getRootList.get(1)).getTitle() +"',";
					result = result + "url:'"+ ((Root_menu)getRootList.get(1)).getUrl() + "',";
					result = result + "leaf:true},";
				}
				//资产出售
				if(r[8]==1||r[9]==1||r[10]==1||r[11]==1){
					result = result + "{id:'" + ((Root_menu)getRootList.get(2)).getId()+"',";
					result = result + "text:'"+((Root_menu)getRootList.get(2)).getTitle() +"',";
					result = result + "url:'"+ ((Root_menu)getRootList.get(2)).getUrl() + "',";
					result = result + "leaf:true},";
				}
				//资产挑拨
				if(r[12]==1||r[13]==1||r[14]==1||r[15]==1){
					result = result + "{id:'" + ((Root_menu)getRootList.get(3)).getId()+"',";
					result = result + "text:'"+((Root_menu)getRootList.get(3)).getTitle() +"',";
					result = result + "url:'"+ ((Root_menu)getRootList.get(3)).getUrl() + "',";
					result = result + "leaf:true},";
				}
				//资产维修
				if(r[16]==1||r[17]==1||r[18]==1||r[19]==1){
					result = result + "{id:'" + ((Root_menu)getRootList.get(4)).getId()+"',";
					result = result + "text:'"+((Root_menu)getRootList.get(4)).getTitle() +"',";
					result = result + "url:'"+ ((Root_menu)getRootList.get(4)).getUrl() + "',";
					result = result + "leaf:true},";
				}*/
			}
/*			if(tree_menu_id.equals(((Tree_menu)getTreeList.get(2)).getId())){
				//折扣明细
				if(r[20]==1){
					result = result + "{id:'" + ((Root_menu)getRootList.get(1)).getId()+"',";
					result = result + "text:'"+((Root_menu)getRootList.get(1)).getTitle() +"',";
					result = result + "url:'"+ ((Root_menu)getRootList.get(1)).getUrl() + "',";
					result = result + "leaf:true},";
				}
				//挑拨明细
				if(r[21]==1){
					result = result + "{id:'" + ((Root_menu)getRootList.get(2)).getId()+"',";
					result = result + "text:'"+((Root_menu)getRootList.get(2)).getTitle() +"',";
					result = result + "url:'"+ ((Root_menu)getRootList.get(2)).getUrl() + "',";
					result = result + "leaf:true},";
				}
				//维修统计
				if(r[22]==1){
					result = result + "{id:'" + ((Root_menu)getRootList.get(3)).getId()+"',";
					result = result + "text:'"+((Root_menu)getRootList.get(3)).getTitle() +"',";
					result = result + "url:'"+ ((Root_menu)getRootList.get(3)).getUrl() + "',";
					result = result + "leaf:true},";
				}
				//固定资产明细
				if(r[23]==1){
					result = result + "{id:'" + ((Root_menu)getRootList.get(0)).getId()+"',";
					result = result + "text:'"+((Root_menu)getRootList.get(0)).getTitle() +"',";
					result = result + "url:'"+ ((Root_menu)getRootList.get(0)).getUrl() + "',";
					result = result + "leaf:true},";
				}
			}
			if(tree_menu_id.equals(((Tree_menu)getTreeList.get(1)).getId())){
				//购置申请
				if(r[24]==1||r[25]==1||r[26]==1||r[27]==1){
					result = result + "{id:'" + ((Root_menu)getRootList.get(0)).getId()+"',";
					result = result + "text:'"+((Root_menu)getRootList.get(0)).getTitle() +"',";
					result = result + "url:'"+ ((Root_menu)getRootList.get(0)).getUrl() + "',";
					result = result + "leaf:true},";
				}
				//购置审批
				if(r[28]==1){
					result = result + "{id:'" + ((Root_menu)getRootList.get(1)).getId()+"',";
					result = result + "text:'"+((Root_menu)getRootList.get(1)).getTitle() +"',";
					result = result + "url:'"+ ((Root_menu)getRootList.get(1)).getUrl() + "',";
					result = result + "leaf:true},";
				}
			}
			if(tree_menu_id.equals(((Tree_menu)getTreeList.get(3)).getId())){
				//字典管理
				if(r[29]==1||r[30]==1){
					result = result + "{id:'" + ((Root_menu)getRootList.get(0)).getId()+"',";
					result = result + "text:'"+((Root_menu)getRootList.get(0)).getTitle() +"',";
					result = result + "url:'"+ ((Root_menu)getRootList.get(0)).getUrl() + "',";
					result = result + "leaf:true},";
				}
			}
			if(tree_menu_id.equals(((Tree_menu)getTreeList.get(4)).getId())){
				//用户管理
				if(r[31]==1||r[32]==1||r[33]==1||r[34]==1){
					result = result + "{id:'" + ((Root_menu)getRootList.get(0)).getId()+"',";
					result = result + "text:'"+((Root_menu)getRootList.get(0)).getTitle() +"',";
					result = result + "url:'"+ ((Root_menu)getRootList.get(0)).getUrl() + "',";
					result = result + "leaf:true},";
				}
				//角色管理
				if(r[35]==1||r[36]==1||r[37]==1||r[38]==1){
					result = result + "{id:'" + ((Root_menu)getRootList.get(1)).getId()+"',";
					result = result + "text:'"+((Root_menu)getRootList.get(1)).getTitle() +"',";
					result = result + "url:'"+ ((Root_menu)getRootList.get(1)).getUrl() + "',";
					result = result + "leaf:true},";
				}
			}*/
			if(tree_menu_id.equals(((Tree_menu)getTreeList.get(5)).getId())){
				//字典管理
				if(r[31]==1||r[32]==1||r[33]==1||r[34]==1||r[35]==1||r[36]==1||r[37]==1||r[38]==1){
					result = result + "{id:'" + ((Root_menu)getRootList.get(0)).getId()+"',";
					result = result + "text:'"+((Root_menu)getRootList.get(0)).getTitle() +"',";
					result = result + "url:'"+ ((Root_menu)getRootList.get(0)).getUrl() + "',";
					result = result + "leaf:true},";
				}
			}
			String res = result.substring(0, result.lastIndexOf(","));			
			out.print("[" + res + "]");
            out.flush();					
		}	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
